package leetcode.p11旋转数组;

/**
 * @author chedx
 * @data 2021/3/16 9:09 AM
 */
public class Solution {
    public static void main(String[] args) {
        int b[] = new int []{3,4,5,6,1,2};
        System.out.println(getMin(b));
        System.out.println(getMax(b));
    }

    /***
     *     /获取最小值
     *     中间的和高位的比较，找出最小值的范围，然后如果中间的和高位相等，那么让高位减1
     *
     */

    public static int getMin(int []num) {
        int low = 0;
        int high = num.length -1;

        while (low < high) {
            int mid = low + (high - low)/2;
            if(num[mid] < num[high]) {
                high = mid;
            }else if(num[mid] > num[high]){
                low = mid+1;
            }else {
                high -= 1;
            }
        }

        return num[high];
    }

    /**
     * 获取最大值
     * 中间的和low的比较，找出最大值范围，如果中间的和高位相等让，low+1
     *
     *
     * */

    public static int getMax(int [] num) {
        int low = 0;
        int high = num.length -1 ;
        while (low < high) {
            int mid = low + (high - low)/2;
            if(num[mid] < num[low]) {
                high = mid-1;
            }else if (num[mid] > num[low]) {
                low = mid;
            }else {
                low += 1;
            }
        }
        return num[low];
    }
}